package com.crm.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Transaction;

import com.crm.dao.base.BaseHibernateDAO;
import com.crm.po.Buy;


public class BuyDAO extends BaseHibernateDAO {

	private static Logger logger = Logger.getLogger(BuyDAO.class);

	public void save(Buy buy) {
		if(buy.getTid() == null || buy.getTid().trim().length() == 0){
			buy.setTid(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {
			this.getSession().save(buy);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public void saveOrUpdate(Buy buy) {
		if(buy.getTid() == null || buy.getTid().trim().length() == 0){
			buy.setTid(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {						
			this.getSession().saveOrUpdate(buy);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}
	
	public void delete(Buy buy) {
		Transaction transaction = getSession().beginTransaction();
		try {
			this.getSession().delete(buy);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public Buy findById(String id) {
		Buy buy = null;
		try {
			buy = (Buy) this.getSession().get(Buy.class, id);
		} catch (RuntimeException e) {
			logger.error(e);
		} finally {
			this.closeSession();	
		}
		return buy;
	}
	
	public List<Buy> buyList(String hql) {
		return (List<Buy>) findList(hql);
	}

	public List<Buy> buyListPage(String hql, int start, int limit) {
		return (List<Buy>) findListPage(hql, start, limit);
	}

}